<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use \app\common\services\applog\AppLogService;
class ErrorController extends Controller
{
	
    public function actionError(){

    	$error=\Yii::$app->errorHandler->exception;
    	$err_msg='';
    	if($error)
    	{
    		$file=$error->getFile();
    		$line=$error->getLine();
    		$message=$error->getMessage();
    		$code=$error->getCode();
    		$file=\Yii::$app->getRuntimePath()."/logs/err.log";
    		// echo $file;

    		// $log=new FileTarget();
    		// $log->logFile=\Yii::$app->getRuntimePath()."/logs/err.log";
    		
    		$err_msg.=$message."[file:{$file} [line:{$line}] [code:{$code}] [url:{$_SERVER['REQUEST_URL']}] [POST_DATA:".http_build_query($_POST)."]";
    		$msg="--------------------".date('y-m-d',time())."--------------\n".$err_msg;
    		file_put_contents($file, $msg,FILE_APPEND);
    		// $log->message[]=[
    		// 	$err_msg,
    		// 	1,
    		// 	'application',
    		// 	microtime(true)
    		// ];
    		// $log->export();
    		//todo写入到数据库
            AppLogService::addErrorLog(\Yii::$app->id,$err_msg);
    	}
        $this->layout=false;
        return $this->render("index",["err_msg"=>$err_msg]);
    }
}
